package dao_manager;

import java.text.SimpleDateFormat;





import beans.ClientInfo;
import beans.Commentaire;
import dao_hibernate.GenericDAOImpl;
import dao_hibernate.HibernateUtil;

public class ClientInfoManagerImpl implements ClientInfoManager{
    
    private GenericDAOImpl<ClientInfo, Long> clientInfoDao = new GenericDAOImpl<ClientInfo, Long>() {};
    ClientInfo clientInfo= null;

    @Override
    public ClientInfo findClientById( Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        }
        return clientInfo;
        
    }
    
    @Override
    public void changeNomFiliale( String nomFiliale, Long client_id ) {
        
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setNomFiliale( nomFiliale );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

   

    @Override
    public void changeNomCorrespondant( String nomCorrespondant, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setNomCorrespondant( nomCorrespondant );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
       
        
    }

    @Override
    public void changeDateEntreeContact( String dateEntreeContact, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setDateEntreeContact( new SimpleDateFormat("yyyy-MM-dd").parse( dateEntreeContact ) );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void changeDateProchainContact( String dateProchainContact, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setDateProchainContact( new SimpleDateFormat("yyyy-MM-dd").parse( dateProchainContact ) );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void changeDateDernierContact( String dateDernierContact, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setDateDernierContact( new SimpleDateFormat("yyyy-MM-dd").parse( dateDernierContact ) );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void changeStatut( String statut, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setStatut( statut );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void changeTelephone( String telephone, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setTelephone( telephone );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void changePoste( String poste, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setPoste( poste );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void changeMail( String mail, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setMail( mail );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void changePays( String pays, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setPays( pays );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void changeZone( String zone, Long client_id ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.setZone( zone );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
    }

    @Override
    public void saveCommentaire( Commentaire commentaire, Long client_id ) {
        GenericDAOImpl<Commentaire, Long> commentaireDao = new GenericDAOImpl<Commentaire, Long>() {};
        try {
            HibernateUtil.beginTransaction();
            commentaireDao.save( commentaire );
            clientInfo = clientInfoDao.findByID( ClientInfo.class, client_id );
            clientInfo.getListComment().add( commentaire );
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        } 
        HibernateUtil.closing();
        
        
    }

    @Override
    public void saveClientInfo( ClientInfo client_info ) {
        try {
            HibernateUtil.beginTransaction();
            clientInfoDao.save(client_info);
            HibernateUtil.commitTransaction();
        } catch (Exception ex) {
            ex.printStackTrace();
            HibernateUtil.rollback();
        }
        HibernateUtil.closing();        
    }

}
